from run import db
from datetime import datetime


class Category(db.Model):
    __tablename__ = 'category'
    id = db.Column(db.INTEGER, primary_key=True)
    categoryname = db.Column(db.String)
    sort = db.Column(db.INTEGER)

    def __init__(self, categoryname, sort):
        self.categoryname = categoryname
        self.sort = sort

    def __repr__(self):
        return '<{}>'.format(self.categoryname)

    def to_json(self):
        return dict(id=self.id, categoryname=self.categoryname)


class Article(db.Model):
    __tablename__ = 'article'
    id = db.Column(db.INTEGER, primary_key=True)
    title = db.Column(db.String)
    author = db.Column(db.String)
    preface = db.Column(db.String)
    faceimage = db.Column(db.String)
    body = db.Column(db.String)
    label = db.Column(db.String)
    isshow = db.Column(db.BOOLEAN)
    ispush = db.Column(db.BOOLEAN)
    numbrowse = db.Column(db.INTEGER)
    releasetime = db.Column(db.DATETIME)
    categoryid = db.Column(db.INTEGER, db.ForeignKey(Category.id))
    category = db.relationship('Category', backref=db.backref('articles', lazy='dynamic'))

    def __init__(self, title=None, author=None, preface=None, body=None, faceimage=None, label=None, isshow=None,
                 ispush=None, numbrowse=0,
                 releasetime=datetime.now()):
        self.title = title
        self.author = author
        self.preface = preface
        self.faceimage = faceimage
        self.body = body
        self.label = label
        self.isshow = isshow
        self.ispush = ispush
        self.numbrowse = numbrowse
        self.releasetime = releasetime

    def __repr__(self):
        return '<编号：{} 作者：{} 摘要：{} 发布时间：{}>'.format(self.id, self.author, self.preface, self.releasetime)

    def to_json(self):
        title = self.title
        if title >= u'\u4e00' and title <= u'\u9fa5':
            if len(title) > 6:
                title = title[0:6] + '...'
        else:
            if len(title) > 8:
                title = title[0:8] + '...'
        return dict(id=self.id, title=title, releasetime=self.releasetime.strftime('%Y-%m-%d'))


class Comment(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.INTEGER, primary_key=True)
    name = db.Column(db.String)
    ip = db.Column(db.String)
    body = db.Column(db.String)
    checkstate = db.Column(db.INTEGER)
    releasetime = db.Column(db.DATETIME)
    article_id = db.Column(db.INTEGER)

    def __init__(self, name=None, ip=None, body=None, checkstate=None, releasetime=datetime.now()):
        self.name = name
        self.ip = ip
        self.body = body
        self.checkstate = checkstate
        self.releasetime = releasetime

    def __repr__(self):
        return '<{}:评论了 {} {}>'.format(self.name, self.body, self.releasetime)


class Message(db.Model):
    __tablename__ = 'message'
    id = db.Column(db.INTEGER, primary_key=True)
    name = db.Column(db.String)
    email = db.Column(db.String)
    body = db.Column(db.String)
    releasetime = db.Column(db.DATETIME)

    def __init__(self, name=None, email=None, body=None, releasetime=datetime.now()):
        self.name = name
        self.email = email
        self.body = body
        self.releasetime = releasetime

    def __repr__(self):
        return '姓名->{} 消息内容->{}'.format(self.name, self.body)
